﻿using System;
using System.Data.Entity;
using System.Linq;
using System.Web.Mvc;
using MvcContrib.UI.Grid;
using QDT.Common;
using QDT.Core.Members;
using MvcContrib.Sorting;
using QDT.Common.Data;

namespace QDT.Web.Admin.Controllers
{
    public class ActivityController : BaseController
    {

        public ActionResult TurnTable(int? page, GridSortOptions sort, int? userid, string username, bool? issend)
        {
            const int pageSize = 50;
            var pageNumber = page ?? 1;

            var filter = PredicateExtensionses.True<UserPrize>();

            if (userid.HasValue) filter = filter.And(x => x.UserId == userid);
            if (!string.IsNullOrWhiteSpace(username)) filter = filter.And(x => x.UserName.Contains(username));
            if (issend.HasValue) filter = filter.And(x => x.IsSend.Equals(issend));

            ViewData["sort"] = sort;
            var source = DbProvider.D<UserPrize>().Where(filter).OrderBy(sort.Column, sort.Direction == SortDirection.Descending);

            return View(source.ToPagedList(pageNumber - 1, pageSize));
        }

        public ActionResult GivePrize(int id)
        {
            var prize = DbProvider.GetByID<UserPrize>(id);
            if (prize == null) return Error();

            prize.IsSend = true;
            prize.SendTime = DateTime.Now;
            DbProvider.SaveChanges();
            
            return Success("操作成功");
        }

        public ActionResult PointSelect(int? page, GridSortOptions sort, int? userid, string username, string email, string mobile, string realname, int? groupId, DateTime? createTime)
        {
            const int pageSize = 50;
            var pageNumber = page ?? 1;

            var groups = DbProvider.D<UserGroup>().ToList();

            ViewData["UserGroups"] = groups;

            var filter = PredicateExtensionses.True<User>();

            if (createTime != null)
            {
                var startTime = ((DateTime)createTime).Date;
                var emdTime = startTime.AddDays(1);
                filter = filter.And(x => x.CreateTime >= startTime && x.CreateTime < emdTime);
            }

            if (userid.HasValue) filter = filter.And(user => user.ID == userid);
            if (groupId.HasValue && groupId != -1) filter = filter.And(user => user.GroupID == groupId);
            if (!string.IsNullOrWhiteSpace(username)) filter = filter.And(user => user.UserName.Contains(username));
            if (!string.IsNullOrWhiteSpace(email)) filter = filter.And(user => user.Email.Contains(email));
            if (!string.IsNullOrWhiteSpace(mobile)) filter = filter.And(user => user.Mobile.Contains(mobile));
            if (!string.IsNullOrWhiteSpace(realname)) filter = filter.And(user => user.RealName.Contains(realname));

            ViewData["sort"] = sort;
            var source = DbProvider.D<User>().Include(x => x.UserAccount).Where(filter.And(user => !user.IsDelete)).OrderBy(sort.Column,
                                                                                                sort.Direction == SortDirection.Descending);
            return View(source.ToPagedList(pageNumber - 1, pageSize));
        }

    }
}
